<template>
  <div class="superManage">
    <el-button type="primary" @click="addManage" style="margin: 10px"
    >添加管理员
    </el-button
    >
    <!--    对话框-->
    <el-dialog title="信息" :visible.sync="dialogFormVisible" :destroy-on-close="true">
      <el-form :model="addUserForm">
        <!--        userName-->
        <el-form-item label="用户名：" :label-width="formLabelWidth">
          <el-input v-model="addUserForm.userName" auto-complete="off"></el-input>
        </el-form-item>
        <!--        password-->
        <el-form-item label="密码：" :label-width="formLabelWidth">
          <el-input v-model="addUserForm.password" auto-complete="off"></el-input>
        </el-form-item>
        <!--        nickName-->
        <el-form-item label="姓名：" :label-width="formLabelWidth">
          <el-input v-model="addUserForm.nickName" auto-complete="off"></el-input>
        </el-form-item>
        <!--        phonenumber-->
        <el-form-item label="手机：" :label-width="formLabelWidth">
          <el-input v-model="addUserForm.phonenumber" auto-complete="off"></el-input>
        </el-form-item>
        <!--        type-->
        <el-form-item label="类型" :label-width="formLabelWidth">
          <el-select v-model="addUserForm.type" placeholder="请选择用户类型">
            <el-option label="普通用户" value="1"></el-option>
            <el-option label="管理员" value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmClick">确 定</el-button>
      </div>
    </el-dialog>
    <el-card>
      <div slot="header">
        <template>
          <div style="text-align: center">管理员权限</div>
        </template>
      </div>
      <div>
        <el-collapse v-model="activeNames" @change="handleChange">
          <el-collapse-item
              :title="item.nickName"
              :name="index"
              v-for="(item, index) in adminData"
              :key="index"
          >
            <div class="activeContent">
              <span>
                <el-tag>{{ item.type }}</el-tag>
              </span>
              <el-button type="danger" @click="deleteManage(item.id)">删除</el-button>
            </div>
          </el-collapse-item>
        </el-collapse>
      </div>
    </el-card>
  </div>
</template>

<script>
import {reqgetAdmin} from "@/api";
import {reqDelete} from "@/api";
import {reqAddUser} from "@/api"

export default {
  data() {
    return {
      activeNames: [],
      adminData: [],
      dialogFormVisible: false,
      addUserForm: {
        userName: '',
        password: '',
        nickName: '',
        phonenumber: '',
        type: "",
      },
      formLabelWidth: '120px'
    };
  },
  created() {
    this.getAdmin()
  },
  mounted() {
  },
  methods: {
    async getAdmin() {
      let result = await reqgetAdmin();
      // console.log(result);
      this.adminData = result;
    },
    handleChange(val) {
      console.log(val);
    },
    //点击添加按钮显示表单
    addManage() {
      this.dialogFormVisible = true;

    },
    //弹出框按钮事件
    async confirmClick() {
      // console.log(this.addUserForm)
      let result = await reqAddUser(this.addUserForm);
      this.$message({
        type: "success",
        message: result,
      });
      this.dialogFormVisible = false;
      this.getAdmin();
      this.clerAddForm();
    },
    //删除用户
    async deleteManage(id) {
      console.log(id)
      let result = await reqDelete(id);
      // console.log(result);
      this.getAdmin();
    },
    //清空表单内容
    clerAddForm() {
      this.addUserForm = {
        userName: '',
        password: '',
        nickName: '',
        phonenumber: '',
        type: "",
      }
    }
  },
};
</script>
<style>
.superManage {
  width: 900px;
}

.activeContent {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}
</style>
